package com.ruoyi.park.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.park.domain.BuiBuilding;
import com.ruoyi.park.mapper.BuiBuildingMapper;
import com.ruoyi.park.mapper.BuiRoomMapper;
import com.ruoyi.park.service.IBuiBuildingService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 楼栋Service业务层处理
 *
 * @author cjie
 * @date 2021-07-20
 */
@Service
@RequiredArgsConstructor
public class BuiBuildingServiceImpl extends LinkFileServiceImpl implements IBuiBuildingService {
    private final BuiBuildingMapper buiBuildingMapper;

    private final BuiRoomMapper buiRoomMapper;

    /**
     * 查询楼栋
     *
     * @param id 楼栋ID
     * @return 楼栋
     */
    @Override
    public BuiBuilding selectBuiBuildingById(String id) {
        BuiBuilding buiBuilding = buiBuildingMapper.selectBuiBuildingById(id);
        buiBuilding.setFileList(getFileList(id));
        return buiBuilding;
    }

    /**
     * 查询楼栋列表
     *
     * @param buiBuilding 楼栋
     * @return 楼栋
     */
    @Override
    public List<BuiBuilding> selectBuiBuildingList(BuiBuilding buiBuilding) {
        return buiBuildingMapper.selectBuiBuildingList(buiBuilding);
    }

    /**
     * 新增楼栋
     *
     * @param buiBuilding 楼栋
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertBuiBuilding(BuiBuilding buiBuilding) {
        buiBuilding.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        buiBuilding.setCreateTime(DateUtils.getNowDate());
        buiBuilding.setCreateBy(SecurityUtils.getUsername());

        if(UserConstants.NOT_UNIQUE.equals(checkBuildingNumUnique(buiBuilding))){
            return AjaxResult.error("当前楼栋编号已存在");
        }

        linkFile(buiBuilding.getFileIdList(), buiBuilding.getId());
        return buiBuildingMapper.insertBuiBuilding(buiBuilding) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改楼栋
     *
     * @param buiBuilding 楼栋
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateBuiBuilding(BuiBuilding buiBuilding) {

        if(UserConstants.NOT_UNIQUE.equals(checkBuildingNumUnique(buiBuilding))){
            return AjaxResult.error("当前楼栋编号已存在");
        }
        buiBuilding.setUpdateTime(DateUtils.getNowDate());
        buiBuilding.setUpdateBy(SecurityUtils.getUsername());

        linkFile(buiBuilding.getFileIdList(), buiBuilding.getId());
        return buiBuildingMapper.updateBuiBuilding(buiBuilding) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量删除楼栋
     *
     * @param ids 需要删除的楼栋ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteBuiBuildingByIds(String[] ids) {
        for(String buildingId : ids){
            if(buiRoomMapper.countRoomByBuildIngId(buildingId) > 0 ){
                return AjaxResult.error("楼栋下存在房屋不可删除");
            }
        }

        deleteLinkFile(ids);
        return buiBuildingMapper.deleteBuiBuildingByIds(ids) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除楼栋信息
     *
     * @param id 楼栋ID
     * @return 结果
     */
    @Override
    public int deleteBuiBuildingById(String id) {
        return buiBuildingMapper.deleteBuiBuildingById(id);
    }

    /**
     * 校验楼栋编号是否唯一
     *
     * @param buiBuilding 楼栋信息
     * @return 结果
     */
    @Override
    public String checkBuildingNumUnique(BuiBuilding buiBuilding) {
        BuiBuilding sourceBuilding = buiBuildingMapper.selectBuiBuildingByNum(buiBuilding.getBuildingNum());

        if(sourceBuilding != null && !sourceBuilding.getId().equals(buiBuilding.getId())){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
